﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Connectivity.Extensibility.Framework;
using Autodesk.Connectivity.WebServices;
using Autodesk.Connectivity.Explorer.Extensibility;
using Autodesk.Connectivity.WebServicesTools;
using Autodesk.Connectivity.WebServices.InformationSvc;

//using JustOnesAndZeros.WebServices.Util;

[assembly: ApiVersion("4.0")]
[assembly: ExtensionId("{D75ABF86-FDEE-489a-9EDE-99ABE00B39BF}")]

namespace EffectiveFolderPermissions
{
    public class Command : IExtension
    {
        #region IExtension Members

        public Command()
        {

        }


        WebServiceManager wsm = null;


        public IEnumerable<CommandSite> CommandSites()
        {
            // the right-click menu
            CommandSite folderContextSite = new CommandSite(
                "Autodesk.EffectiveFolderPermissions.FolderContextSite",
                "Effective Folder Permissions")
                {
                    DeployAsPulldownMenu = false,
                    Location = CommandSiteLocation.FolderContextMenu
                };

            CommandItem singleFolderCommand = new CommandItem(
               "Autodesk.EffectiveFolderPermissions.SingleFolderCommand",
               "Effective Folder Permissions")
            {
                Description = "See effective permissions for the selected folder",
                Hint = "",
                Image = Resource.EffectiveFolderPermissions, 
                MultiSelectEnabled = false,
                NavigationTypes = new SelectionTypeId [] {SelectionTypeId.Folder},
                ToolbarPaintStyle = PaintStyle.TextAndGlyph
            };
            singleFolderCommand.Execute += new EventHandler<CommandItemEventArgs>(singleFolderCommand_Execute);
            folderContextSite.AddCommand(singleFolderCommand);


            // An item under the tools menu
            CommandSite toolsSite = new CommandSite(
                "Autodesk.EffectiveFolderPermissions.ToolsSite",
                "Effective Folder Permissions")
            {
                DeployAsPulldownMenu = true,
                Location = CommandSiteLocation.ToolsMenu
            };

            CommandItem overviewCommand = new CommandItem(
                "Autodesk.EffectiveFolderPermissions.OverviewCommand",
                "Permissions Overview")
                {
                    Description = "See effective permissions for a set of folders",
                    Hint = "",
                    Image = Resource.EffectiveFolderPermissions,
                    MultiSelectEnabled = false,
                    NavigationTypes = null,
                    ToolbarPaintStyle = PaintStyle.TextAndGlyph
                };
            overviewCommand.Execute += new EventHandler<CommandItemEventArgs>(overviewCommand_Execute);
            toolsSite.AddCommand(overviewCommand);


            CommandItem adminListCommand = new CommandItem(
                "Autodesk.EffectiveFolderPermissions.AdminListCommand",
                "Administrator List")
            {
                Description = "A list of users with administrative rights",
                Hint = "",
                Image = Resource.EffectiveFolderPermissions,
                MultiSelectEnabled = false,
                NavigationTypes = null,
                ToolbarPaintStyle = PaintStyle.TextAndGlyph
            };
            adminListCommand.Execute += new EventHandler<CommandItemEventArgs>(adminListCommand_Execute);
            toolsSite.AddCommand(adminListCommand);

            CommandItem settingsCommand = new CommandItem(
                "Autodesk.EffectiveFolderPermissions.SettingsCommand",
                "Settings")
            {
                Description = "Effective Folder Permissions settings",
                Hint = "",
                Image = Resource.EffectiveFolderPermissions,
                MultiSelectEnabled = false,
                NavigationTypes = null,
                ToolbarPaintStyle = PaintStyle.TextAndGlyph
            };
            settingsCommand.Execute += new EventHandler<CommandItemEventArgs>(settingsCommand_Execute);
            toolsSite.AddCommand(settingsCommand);

            return new CommandSite[] { folderContextSite, toolsSite };
        }


        public IEnumerable<DetailPaneTab> DetailTabs()
        {
            return null;
        }

        public IEnumerable<string> HiddenCommands()
        {
            return null;
        }

        public void OnLogOff(IApplication application)
        {
            //ServiceManager.ClearSecurityContext();
        }

        public void OnLogOn(IApplication application)
        {
            //ServiceManager.SetSecurityContext(application.VaultContext);
        }

        public void OnShutdown(IApplication application)
        {
            // do nothing
        }

        public void OnStartup(IApplication application)
        {
            // do nothing
        }

        public string ResourceCollectionName()
        {
            return "Resource";
        }

        #endregion

        void settingsCommand_Execute(object sender, CommandItemEventArgs e)
        {
            _InformationService infoSvc = new InformationService();
            VaultContext vc = e.Context.Application.VaultContext;
            wsm = new WebServiceManager(new UserIdTicketCredentials_bugfix(vc.RemoteBaseUrl.ToString().Split(new char[] { '/' })[2], vc.VaultName, vc.UserId, vc.Ticket));
            SettingsForm form = new SettingsForm(Settings.LoadSettings(wsm), wsm);
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
                form.Settings.SaveSettings(wsm);
        }

        void overviewCommand_Execute(object sender, CommandItemEventArgs e)
        {
            //_InformationService infoSvc = new InformationService();
            VaultContext vc = e.Context.Application.VaultContext;

            wsm = new WebServiceManager(new UserIdTicketCredentials_bugfix(vc.RemoteBaseUrl.ToString().Split(new char[] { '/' })[2], vc.VaultName, vc.UserId, vc.Ticket));
            MainForm mainForm = new MainForm(wsm, vc);
            mainForm.ShowDialog();

        }

        void singleFolderCommand_Execute(object sender, CommandItemEventArgs e)
        {
            long folderId = -1;

            foreach (ISelection selection in e.Context.NavSelectionSet)
            {
                if (selection.TypeId == SelectionTypeId.Folder)
                {
                    folderId = selection.Id;
                    break;
                }
            }

            if (folderId > 0)
            {
                _InformationService infoSvc = new InformationService();
                VaultContext vc = e.Context.Application.VaultContext;
                wsm = new WebServiceManager(new UserIdTicketCredentials_bugfix(vc.RemoteBaseUrl.ToString().Split(new char[] { '/' })[2], vc.VaultName, vc.UserId, vc.Ticket));
                MainForm mainForm = new MainForm(folderId, wsm, vc);
                mainForm.ShowDialog();
            }
        }

        void adminListCommand_Execute(object sender, CommandItemEventArgs e)
        {
            _InformationService infoSvc = new InformationService();
            VaultContext vc = e.Context.Application.VaultContext;
            wsm = new WebServiceManager(new UserIdTicketCredentials_bugfix(vc.RemoteBaseUrl.ToString().Split(new char[] { '/' })[2], vc.VaultName, vc.UserId, vc.Ticket));
            MainForm mainForm = new MainForm(wsm, vc);
            AdminUsersForm adminForm = new AdminUsersForm(mainForm.Users);
            adminForm.ShowDialog();
        }
    }
}
